package cn.htcode.service.imp;

import cn.htcode.domain.User;
import cn.htcode.mapper.UserMapper;
import cn.htcode.service.IRegisterService;
import cn.htcode.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.io.IOException;

/**
 * @author: Mr_he
 * @Version:1.0
 */
public class RegisterImpl implements IRegisterService {

    @Override
    public boolean checkAccount(String account) {
        try (SqlSession sqlSession = MybatisUtil.getSqlSession()) {
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            //判断用户是否存在
            User user = mapper.selectByaccount(account);
            return user != null;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public int insert(String account, String password) {
        try (SqlSession sqlSession = MybatisUtil.getSqlSession()) {
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            User user1 = new User();
            user1.setAccount(account);
            user1.setPassword(password);
            int insert = mapper.insert(user1);
            return insert;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return 0;
    }
}
